local extension = Package("fk_study")

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["fk_study"] = "fk学习包",
}

-- local cdwsunce = General(extension, "cdwsunce", "wu", 4)
-- Fk:loadTranslationTable{
--   ["cdwsunce"] = "孙伯符",
-- }


local cdw_ex__zuoci = General(extension, "cdw_ex__zuoci", "qun", 7)

local huashen_blacklist = {
  -- imba
  "zuoci", "cdw_ex__zuoci", "qyt__dianwei", "starsp__xiahoudun", "mou__wolong",
  -- haven't available skill
  "js__huangzhong", "liyixiejing", "olz__wangyun", "yanyan", "duanjiong", "wolongfengchu", "wuanguo", "os__wangling", "tymou__jiaxu",
}
local function Gethuashen(player, n)
  local room = player.room
  local generals = table.filter(room.general_pile, function (name)
    return not table.contains(huashen_blacklist, name)
  end)
  local mark = U.getPrivateMark(player, "&cdw_ex__huashen")
  for _ = 1, n do
    if #generals == 0 then break end
    local g = table.remove(generals, math.random(#generals))
    table.insert(mark, g)
    table.removeOne(room.general_pile, g)
  end
  U.setPrivateMark(player, "&cdw_ex__huashen", mark)
end
local function Dohuashen(player)
  local room = player.room
  local mark = U.getPrivateMark(player, "&cdw_ex__huashen")
  if #mark == 0 then return end
  local name = room:askForGeneral(player, mark, 1, true)
  local general = Fk.generals[name]
  room:setPlayerMark(player, "cdw_ex__huashen_general", name)
  local original_general = player.general
  player.general = name
  room:broadcastProperty(player, "general")
  player.gender = general.gender
  room:broadcastProperty(player, "gender")
  player.kingdom = general.kingdom
  room:askForChooseKingdom({player})
  room:broadcastProperty(player, "kingdom")
--   local old_skill = player:getMark("@cdw_ex__huashen_skill")
  local old_skill = 0 --*****************************************
  if old_skill ~= 0 then room:handleAddLoseSkills(player, "-"..old_skill) end
  local skills = {}
  for _, skillName in ipairs(general:getSkillNameList()) do
    local s = Fk.skills[skillName]
    if not (s.lordSkill or s.switchSkillName or s.frequency > 3 ) then
      if #s.attachedKingdom == 0 or table.contains(s.attachedKingdom, player.kingdom) then
        table.insert(skills, skillName)
      end
    end
  end
  if #skills > 0 then
    local choice = room:askForChoice(player, skills, "cdw_ex__huashen", "#cdw_ex__huashen-skill", true)
    room:setPlayerMark(player, "@cdw_ex__huashen_skill", choice)
    room:handleAddLoseSkills(player, choice)
  else
    room:setPlayerMark(player, "@cdw_ex__huashen_skill", 0)
  end
  room:delay(500)
  player.general = original_general
  room:broadcastProperty(player, "general")









    local generals = U.getPrivateMark(player, "&cdw_ex__huashen")
    if #generals < 3 then return end
    local current_general = type(player:getMark("cdw_ex__huashen_general")) == "string" and player:getMark("cdw_ex__huashen_general") or ""
    local result = player.room:askForCustomDialog(player, "cdw_ex__huashen",
    "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
      generals,
      {"OK"},
      "#cdw_ex__huashen-recast",
      {"Cancel"},
      1,
      2, --**************************************
      {current_general},
    })
    if result == "" then return end
    local reply = json.decode(result)
    if reply.choice ~= "OK" then return end
    local removed = reply.cards
    for _, g in ipairs(removed) do
      table.removeOne(generals, g)
    end
    U.setPrivateMark(player, "&cdw_ex__huashen", generals)
    -- Gethuashen(player, #removed)
    room:returnToGeneralPile(removed)
end
local function Recasthuashen(player)
  local room = player.room
  local generals = U.getPrivateMark(player, "&cdw_ex__huashen")
  if #generals < 3 then return end
  local current_general = type(player:getMark("cdw_ex__huashen_general")) == "string" and player:getMark("cdw_ex__huashen_general") or ""
  local result = player.room:askForCustomDialog(player, "cdw_ex__huashen",
  "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
    generals,
    {"OK"},
    "#cdw_ex__huashen-recast",
    {"Cancel"},
    1,
    2, --**************************************
    {current_general},
  })
  if result == "" then return end
  local reply = json.decode(result)
  if reply.choice ~= "OK" then return end
  local removed = reply.cards
  for _, g in ipairs(removed) do
    table.removeOne(generals, g)
  end
  U.setPrivateMark(player, "&cdw_ex__huashen", generals)
  Gethuashen(player, #removed)
  room:returnToGeneralPile(removed)
end
local cdw_ex__huashen = fk.CreateTriggerSkill{
  name = "cdw_ex__huashen",
  events = {fk.GameStart, fk.TurnStart, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and target == player and #U.getPrivateMark(player, "&cdw_ex__huashen") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GameStart then return true end
    local choice = player.room:askForChoice(player, {"cdw_ex__huashen_re", "cdw_ex__huashen_recast", "Cancel"},self.name)
    if choice ~= "Cancel" then
      self.cost_data = choice
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.GameStart then
      Gethuashen(player, 5) --*****************************************
      Dohuashen(player)
    else
      if self.cost_data == "cdw_ex__huashen_re" then
        Dohuashen(player)
      else
        Recasthuashen(player)
      end
    end
  end,

  refresh_events = {fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    return player == target and data == self
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@[private]&cdw_ex__huashen", 0)
  end,
}
local cdw_ex__xinsheng = fk.CreateTriggerSkill{
  name = "cdw_ex__xinsheng",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:hasSkill(cdw_ex__huashen, true)
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for _ = 1, data.damage do 
      if self.cancel_cost or not (player:hasSkill(self) and player:hasSkill(cdw_ex__huashen, true)) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player, self.name, data) then return true end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    Gethuashen(player, 1)
    -- Gethuashen(player, 1) --******************************************
  end,
}
cdw_ex__zuoci:addSkill(cdw_ex__huashen)
cdw_ex__zuoci:addSkill(cdw_ex__xinsheng)
Fk:loadTranslationTable{
  ["cdw_ex__zuoci"] = "界左慈",
  ["#cdw_ex__zuoci"] = "迷之仙人",
  ["illustrator:cdw_ex__zuoci"] = "波子",
  ["cdw_ex__huashen"] = "化身",
  [":cdw_ex__huashen"] = "①游戏开始时，你随机获得三张武将牌作为“化身”牌，然后你选择其中一张“化身”牌的一个技能（主公技/限定技/觉醒技/转换技除外），你视为拥有此技能，且性别和势力视为与此“化身”牌相同。<br>"..
  "②回合开始或结束时，你可以选择一项：1.重新进行一次“化身”；2.移去至多三张不为亮出的“化身”牌，然后获得等量的新“化身”牌。",
  ["@[private]&cdw_ex__huashen"] = "化身",
  ["@cdw_ex__huashen_skill"] = "化身",
  ["cdw_ex__huashen_re"] = "进行一次“化身”",
  ["cdw_ex__huashen_recast"] = "移去至多三张“化身”，获得等量新“化身”",
  ["#cdw_ex__huashen-recast"] = "移去至多三张“化身”，获得等量新“化身”",
  ["#cdw_ex__huashen-skill"] = "化身：选择获得的技能",
  ["cdw_ex__xinsheng"] = "新生",
  [":cdw_ex__xinsheng"] = "当你受到1点伤害后，若你有技能“化身”，你可以随机获得一张新的“化身”牌。",

  ["$cdw_ex__huashen1"] = "容貌发肤，不过浮尘。",
  ["$cdw_ex__huashen2"] = "皮囊万千，吾皆可化。",
  ["$cdw_ex__xinsheng1"] = "枯木发荣，朽木逢春。",
  ["$cdw_ex__xinsheng2"] = "风靡云涌，万丈光芒。",
  ["~cdw_ex__zuoci"] = "红尘看破，驾鹤仙升……",
}

return { extension }
-- 3291211686@qq.com

-- local room = player.room
--   local generals = U.getPrivateMark(player, "&cdw_ex__huashen")
--   local current_general = type(player:getMark("cdw_ex__huashen_general")) == "string" and player:getMark("cdw_ex__huashen_general") or ""
--   local result = player.room:askForCustomDialog(player, "cdw_ex__huashen",
--   "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
--     generals,
--     {"OK"},
--     "#cdw_ex__huashen-recast",
--     {"Cancel"},
--     1,
--     2,
--     {current_general},
--   })
--   if result == "" then return end
--   local reply = json.decode(result)
--   if reply.choice ~= "OK" then return end
--   local removed = reply.cards
--   for _, g in ipairs(removed) do
--     table.removeOne(generals, g) 

--   end